package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.RecordRequest;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.RecordMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.Record;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 中奖记录业务层处理
 *
 * @author mars
 * @date 2024-04-23
 */
@Slf4j
@Service
@AllArgsConstructor
public class RecordServiceImpl implements IRecordService {

    private final RecordMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Record add(RecordRequest request) {
        Record entity = Record.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(RecordRequest request) {
        Record entity = Record.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public Record getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<Record> pageList(RecordRequest request) {
        Page<Record> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<Record> query = this.buildWrapper(request);
        IPage<Record> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<Record> list(RecordRequest request) {
        LambdaQueryWrapper<Record> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<Record> buildWrapper(RecordRequest param) {
        LambdaQueryWrapper<Record> query = new LambdaQueryWrapper<>();
        if (param.getMemberId() != null){
               query.eq(Record::getUserId,param.getMemberId());
        }
        if (param.getActivityId() != null){
               query.eq(Record::getActivityId ,param.getActivityId());
        }
        if (param.getAwardId() != null){
               query.eq(Record::getAwardId ,param.getAwardId());
        }
        if (param.getIsAward() != null){
               query.eq(Record::getIsAward ,param.getIsAward());
        }
        if (param.getActivityName() != null){
            query.like(Record::getActivityName ,param.getActivityName());
        }
        if (param.getUserName() != null){
            query.like(Record::getUserName ,param.getUserName());
        }
        if (param.getAwardName() != null){
            query.like(Record::getAwardName ,param.getAwardName());
        }

        return query;
    }

}
